-- NewTeamMemberPanel
-- 对应 Node_player.csd

local NewTeamMemberInfoPanel = import(".NewTeamMemberInfoPanel")
local NewLegionMemberInfoPanel = import(".NewLegionMemberInfoPanel")

local teamMemberNum = 5 -- 队伍最大人数
local legionMemberNum = 10 -- 团队最大人数

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewMemberInfoNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	-- 普通队伍成员信息
	self.teamMemberInfoList = {}
	for i = 1, teamMemberNum do
		local infoTemp = NewTeamMemberInfoPanel.create(self._ui["Panel_team/player0"..i])
		infoTemp:setData( nil )
		infoTemp:registerOnOperate(function(op, ...)
					executeFunc(self._onPanelHandler, op, ...) -- 交由controller处理
				end)
		table.insert( self.teamMemberInfoList, infoTemp )
	end

	-- 军团成员信息
	self.legionMemberInfoList = {}
	for i = 1, legionMemberNum do
		local infoTemp = NewLegionMemberInfoPanel.create(self._ui["Panel_legion/player"..i])
		infoTemp:setData( nil )
		infoTemp:registerOnOperate(function(op, ...)
					executeFunc(self._onPanelHandler, op, ...) -- 交由controller处理
				end)
		table.insert( self.legionMemberInfoList, infoTemp )
	end
end

-- data = TeamVo
function ClassRef:refresh()
	if self._data then -- 有目标队伍
		local data = self._data

		local isLegion = data:isLegion() -- 默认不是军团
		self._ui["Panel_team"]:setVisible( not isLegion )
		self._ui["Panel_legion"]:setVisible( isLegion )
		local renders = isLegion and self.legionMemberInfoList or self.teamMemberInfoList
		self._ui.Panel_team:setVisible(not isLegion)
		self._ui.Panel_legion:setVisible(isLegion)
		self._renders = renders

		-- 正常成员
		local members = data:getMembers()
		local memberNum = #members
		local leader = data:getLeader()
		for i, member in ipairs( members ) do
			renders[i]:setData(member)
			renders[i]:refreshTeamLeaderFlag( member:isLeader() )
			renders[i]:refreshLeaderStates()
		end

		-- 机器人
		local robots = data:getRobots()
		for i,robot in ipairs(robots) do
			renders[memberNum + i]:setRobotInfo(robot)
			renders[memberNum + i]:refreshTeamLeaderFlag( false )
			renders[memberNum + i]:refreshLeaderStates()
		end

		memberNum = memberNum + #robots
		if memberNum < #renders then
			for i=memberNum + 1, #renders do
				renders[i]:setData(nil)
			end
		end
	end
end

-- crossInfo TeamMatchCrossInfo
function ClassRef:refreshCrossMatch(crossInfo)
	local members = crossInfo:getMembers()
	local isLegion = #members > 5
	local renders = isLegion and self.legionMemberInfoList or self.teamMemberInfoList
	self._ui.Panel_team:setVisible(not isLegion)
	self._ui.Panel_legion:setVisible(isLegion)
	self._renders = renders

	local member
	for i,render in ipairs(renders) do
		member = members[i]
		if member then
			render:setBaseInfo(member.baseInfo)
			render:refreshTeamLeaderFlag(false)
			render:setReadyStatus(member.isReady and "yes" or "none" )
		else
			render:setBaseInfo(nil)
		end
	end
end

function ClassRef:getMemberRendres()
	return self._renders
end

-- 根据成员Uid返回Ui中对应队伍成员的render
function ClassRef:getTeamMemberRenderByUid( uid )
	for i,v in ipairs( self.teamMemberInfoList ) do
		local member = v:getData()
		if member and uid == member:getUid() then
			return v
		end
	end
end

-- 根据成员Uid返回Ui中对应军团成员的render
function ClassRef:getLegionMemberRenderByUid( uid )
	for i,v in ipairs( self.legionMemberInfoList ) do
		local member = v:getData()
		if member and uid == member:getUid() then
			return v
		end
	end
end

-- 禁止标志 是否显示
function ClassRef:refreshForbiddenIconStatus( maxNums ) -- maxNums - 玩法允许的最大人数
	for i,v in ipairs( self.teamMemberInfoList ) do
		v:setForbiddenIconVisible( i > maxNums )
	end
	for i,v in ipairs( self.legionMemberInfoList ) do
		v:setForbiddenIconVisible( i > maxNums )
	end
end

function ClassRef:refreshStopIconVisible()
	for i,v in ipairs( self.teamMemberInfoList ) do
		v:refreshStopIconVisible()
	end
	for i,v in ipairs( self.legionMemberInfoList ) do
		v:refreshStopIconVisible()
	end
end

-- 刷新随从信息
function ClassRef:refreshPartnerInfo( maxNums, partnerInfo, readyStatus )
	local partnersNum = #partnerInfo
	local countIndex = 1
	if self._data:isLegion() then
		for i,v in ipairs( self.legionMemberInfoList ) do
			if i <= maxNums and not v:haveData() then
				if countIndex <= partnersNum then
					local p = partnerInfo[countIndex]
					v:setPartnerInfo( p )
					v:setReadyStatus( readyStatus )
					countIndex = countIndex + 1
				else
					break
				end
			end
		end
	else

		
		for i,v in ipairs( self.teamMemberInfoList ) do
			if i <= maxNums and not v:haveData() then -- 人数小于玩法限制的最大人数 && 存在空位置
				-- 则填充队长的随从信息
				if countIndex <= partnersNum then
					local p = partnerInfo[countIndex]
					v:setPartnerInfo( p )
					v:setReadyStatus( readyStatus )
					countIndex = countIndex + 1
				else
					break
				end
			end
		end
	end
end

function ClassRef:registerOnOperate( cb )
	self._onPanelHandler = cb
end

return ClassRef
